package com.bmp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bmp.model.Module;
import com.bmp.model.vo.ProjectVoModule;
import com.bmp.service.ModuleManager;
import com.opensymphony.xwork2.ActionSupport;

public class ModuleAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private ModuleManager moduleManager;
	private Module module;
	
	private JSONObject jsonObject;
	private String page;
	private String rows;
	
	//初始化moduleManager
	public ModuleAction(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		moduleManager = (ModuleManager) ctx.getBean("moduleManager");
	}
	
	//显示模块信息，json格式
	public String moduleInfo(){
		//当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数          
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        //每页的开始记录  第一页为1  第二页为number +1   
        int start = (intPage-1)*number;  
        List<ProjectVoModule> moduleList = moduleManager.getModule(start, number);//每页的数据，放入list        
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total", moduleManager.getTotalPages());//total键 存放总记录数，必须的     
        jsonMap.put("rows", moduleList);//rows键 存放每页记录 list  
        jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
		return "moduleInfo";
	}
	
	//增加模块
	public String addModule(){
		moduleManager.addModule(this.module);
		return "success";
	}
	
	//删除模块
	public String deleteModule(){
		moduleManager.deleteModuleById(this.module.getId());
		return "success";
	}
	
	//更新模块信息
	public String updateModule(){
		moduleManager.updateModule(this.module);
		return "success";
	}
	
	//根据项目ID获取模块信息
	public String getModuleByProId(){
		moduleManager.getModuleByProId(this.module.getPro_id());
		return "success";
	}
	/************************getter and setter********************************/
	public ModuleManager getModuleManager() {
		return moduleManager;
	}
	public void setModuleManager(ModuleManager moduleManager) {
		this.moduleManager = moduleManager;
	}
	public Module getModule() {
		return module;
	}
	public void setModule(Module module) {
		this.module = module;
	}
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
}
